import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
    state: {
        userinfo: {
            username: '',
            account: '21311311',
            password: '',
            permission:'3',
            unread:'0',
            avatar: {
                id: '20',
                url: 'https://xybt-1326728101.cos.ap-guangzhou.myqcloud.com/1_1720293156677.jpg'
            },
            cover: {
                id: '19',
                url: 'https://xybt-1326728101.cos.ap-guangzhou.myqcloud.com/1_1720291307661.jpg'
            }
        },
        activeIndex: '1', // 默认激活的菜单项索引
    },
    mutations: {
        USER_INFO(state, info) {
            state.userinfo.username = info.Uname
            state.userinfo.account = info.UID
            state.userinfo.password = info.Password
            state.userinfo.permission=info.Permission
            state.userinfo.avatar=info.Avatar
            state.userinfo.cover=info.Cover
        },
        USER_NAME(state, name) {
            state.userinfo.username = name
        },
        USER_ACCOUNT(state, acc) {
            state.userinfo.account = acc
        },
        USER_PASSWORD(state, pwd) {
            state.userinfo.password = pwd
        },
        USER_AVATAR(state,avatar){
            state.userinfo.avatar.id=avatar.id
            state.userinfo.avatar.url=avatar.url
        },
        USER_COVER(state,cover){
            console.log("store:cover",cover)
            state.userinfo.cover.id=cover.id
            state.userinfo.cover.url=cover.url
        },
        MSG_UNREAD(state,count){
            state.userinfo.unread=count
        },
        setActiveIndex(state, index) {
            state.activeIndex = index;
        },
    },
    actions: {
        saveUNREAD({ commit }, data){
            commit('MSG_UNREAD', data)
        },
        saveUserInfo({ commit }, data) {
            commit('USER_INFO', data)
        },
        saveUsername({ commit }, data) {
            commit('USER_NAME', data)
        },
        savePassword({ commit }, data) {
            commit('USER_PASSWORD', data)
        },
        saveAccount({ commit }, data) {
            commit('USER_ACCOUNT', data)
        },
        SaveCover({commit},data){
            commit('USER_COVER',data)
        },
        SaveAvatar({commit},data){
            commit('USER_AVATAR',data)
        },
        setIndex({ commit }, index) {
            commit('setActiveIndex', index);
        }
    },
    plugins: [createPersistedState({
        storage: window.localStorage,
        reducer(state) {
            return {
                userinfo: state.userinfo
            };
        }
    })]
});
